package com.hele.sts.webwork;

import com.opensymphony.xwork2.Action;


public abstract class PageAction
  implements Action
{
  public static final int DEFAULT_PAGE_SIZE = 20;
  protected int totalCount;
  int pageNo;
  int totalPage;
  int pageSize;
  
  public PageAction()
  {
    this.pageNo = 1;
    this.pageSize = DEFAULT_PAGE_SIZE;
  }
  
  public String execute()
    throws Exception
  {
    this.totalCount = count();
    this.totalPage = ((this.totalCount - 1) / this.pageSize + 1);
    if (this.pageNo > this.totalPage) {
      this.pageNo = this.totalPage;
    }
    if (this.pageNo < 1) {
      this.pageNo = 1;
    }
    return list();
  }
  
  public abstract String getHref();
  
  public abstract int count();
  
  public abstract String list();
  
  public int getNextPage()
  {
    if (this.pageNo + 1 > this.totalPage) {
      return this.totalPage;
    }
    return this.pageNo + 1;
  }
  
  public int getPrevPage()
  {
    if (this.pageNo - 1 < 1) {
      return 1;
    }
    return this.pageNo - 1;
  }
  
  public int getPageNo()
  {
    return this.pageNo;
  }
  
  public void setPageNo(int pageNo)
  {
    this.pageNo = pageNo;
  }
  
  public int getPageSize()
  {
    return this.pageSize;
  }
  
  public void setPageSize(int pageSize)
  {
    this.pageSize = pageSize;
  }
  
  public int getTotalCount()
  {
    return this.totalCount;
  }
  
  public void setTotalCount(int totalCount)
  {
    this.totalCount = totalCount;
  }
  
  public int getTotalPage()
  {
    return this.totalPage;
  }
  
  public void setTotalPage(int totalPage)
  {
    this.totalPage = totalPage;
  }
}
